package com.thread.thread;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.thread.model.entity.UserDemo;
import com.thread.model.entity.test1.UserAge;
import com.thread.model.entity.test1.UserName;
import com.thread.model.service.IUserAgeService;
import com.thread.model.service.IUserNameService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ccubee
 * @since 21-3-30 21:25
 */
public class ThreadDemo extends Thread {


    private IUserNameService userNameService;
    private IUserAgeService userAgeService;
    private List<UserDemo> list;


    public ThreadDemo(IUserNameService userNameService, IUserAgeService userAgeService, List<UserDemo> list) {
        this.userNameService = userNameService;
        this.userAgeService = userAgeService;
        this.list = list;
    }

    @Override
    public void run() {
        list.forEach(user -> {
            insertUserAge(user);
            insertUserName(user);
        });
    }


    @DS("test1")
    private void insertUserAge(UserDemo userDemo) {
        LocalDateTime now = LocalDateTime.now();
        UserAge userAge = UserAge.builder().userId(userDemo.getId())
                .userAge(userDemo.getAge())
                .userBirth(now)
                .createTime(now)
                .updateTime(now)
                .build();
        userAgeService.save(userAge);
    }

    @DS("test1")
    private void insertUserName(UserDemo userDemo) {
        LocalDateTime now = LocalDateTime.now();
        UserName userName = UserName.builder()
                .userId(userDemo.getId())
                .userName(userDemo.getName())
                .createTime(now)
                .updateTime(now)
                .userPassword(userDemo.getPassword())
                .build();
        userNameService.save(userName);
    }
}
